package com.jake.springboot.order.context;

import com.jake.springboot.order.constant.OrderTypeEnum;
import com.jake.springboot.order.strategy.AbstractStrategy;
import com.jake.springboot.order.util.SpringBeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.Map;

public class StrategyContext {

    private final Map<OrderTypeEnum, Class<?>> strategyMap;

    public StrategyContext(Map<OrderTypeEnum, Class<?>> strategyMap) {
        this.strategyMap = strategyMap;
    }

    public AbstractStrategy getStrategy(OrderTypeEnum orderTypeEnum) {
        if (orderTypeEnum == null) {
            throw new IllegalArgumentException("not found enum");
        }

        if (CollectionUtils.isEmpty(strategyMap)) {
            throw new IllegalArgumentException("strategy map is empty, please check you strategy package path");
        }

        Class<?> clazz = strategyMap.get(orderTypeEnum);
        if (clazz == null) {
            throw new IllegalArgumentException("not found strategy for type:" + orderTypeEnum.getCode());
        }

        return (AbstractStrategy) SpringBeanUtils.getBean(clazz);
    }

}
